package com.ikdong.toodle.activity;

import java.util.Calendar;
import java.util.TimeZone;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
import com.ikdong.toodle.R;
import com.ikdong.toodle.util.ComUtil;
import com.ikdong.toodle.util.Constant;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.TimePicker;

public class TaskAddDateSetActivity extends BasicActivity implements View.OnClickListener{
	public static final int SET_START = 0;
	public static final int SET_DUE = 1;
	public static final int NO_SET = 2;
	public static final String PARAM_DATE = "PARAM_DATE";
	public static final String PARAM_TIME = "PARAM_TIME";
	public static final String PARAM_OPERATION = "PARAM_OPERATION";
	private int operation = -1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.task_date_set);
		operation = this.getIntent().getExtras().getInt(PARAM_OPERATION);
		initView();
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
		getSupportActionBar().setTitle(getString(R.string.actionbar_title_date_set));
		
		MenuItem cancelItem = menu.add(getString(R.string.lable_action_cancel));
		cancelItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		cancelItem.setOnMenuItemClickListener(new OnMenuItemClickListener(){
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				TaskAddDateSetActivity.this.setResult(NO_SET, null);
				finish();
				return true;
			}});
		
		MenuItem noItem = menu.add(getString(R.string.lable_action_no_date));
		noItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		noItem.setOnMenuItemClickListener(new OnMenuItemClickListener(){
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Intent data = new Intent();
				data.putExtra(PARAM_DATE, 0);
				data.putExtra(PARAM_TIME, 0);
				TaskAddDateSetActivity.this.setResult(operation, data);
				finish();
				return true;
			}});
		
		MenuItem setItem = menu.add(getString(R.string.lable_action_set));
		setItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		setItem.setOnMenuItemClickListener(new OnMenuItemClickListener(){
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Intent data = new Intent();
				data.putExtra(PARAM_DATE, getDate());
				data.putExtra(PARAM_TIME, getTime());
				TaskAddDateSetActivity.this.setResult(operation, data);
				finish();
				return true;
			}});
		
		return true;
	}
	
	private void initView(){
		CheckBox enable = (CheckBox) this.findViewById(R.id.ta_task_date_set_time_enable);
		enable.setOnClickListener(this);
		TimePicker tp = (TimePicker) this.findViewById(R.id.ta_task_date_set_time);
		tp.setIs24HourView(true);
		tp.setEnabled(enable.isChecked());
		
		initDateTime();
	}

	@Override
	public void onClick(View v) {
		if(R.id.ta_task_date_set_time_enable == v.getId()){
			CheckBox enable = (CheckBox) this.findViewById(R.id.ta_task_date_set_time_enable);
			findViewById(R.id.ta_task_date_set_time).setEnabled(enable.isChecked());
		}
	}
	
	private long getDate(){
		DatePicker datePicker = (DatePicker) findViewById(R.id.ta_task_date_set_date);
		int year = datePicker.getYear();
		int month = datePicker.getMonth();
		int day = datePicker.getDayOfMonth();
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeZone(TimeZone.getTimeZone("GMT+0"));
		calendar.set(year, month, day);	
		
		return ComUtil.parseUnixStamp(calendar.getTime());
	}
	
	private long getTime(){
		CheckBox enable = (CheckBox) this.findViewById(R.id.ta_task_date_set_time_enable);
		int hour = 0;
		int minute = 0;
		if(enable.isChecked()){
			TimePicker tp = (TimePicker) this.findViewById(R.id.ta_task_date_set_time);
			hour = tp.getCurrentHour();
			minute = tp.getCurrentMinute();
		}
		Log.d(Constant.TAG, "hour is "+hour+", minute is "+minute);
		DatePicker datePicker = (DatePicker) findViewById(R.id.ta_task_date_set_date);
		int year = datePicker.getYear();
		int month = datePicker.getMonth();
		int day = datePicker.getDayOfMonth();
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeZone(TimeZone.getTimeZone("GMT+0"));
		calendar.set(year, month, day,hour,minute);	
		
		
		return ComUtil.parseUnixStamp(calendar.getTime());
	}
	
	private void initDateTime(){
		long date = this.getIntent().getExtras().getLong(PARAM_DATE);
		if(date>0){
			DatePicker datePicker = (DatePicker) findViewById(R.id.ta_task_date_set_date);
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeZone(TimeZone.getTimeZone("GMT+0"));
			calendar.setTimeInMillis(ComUtil.parseDateByUS(date).getTime());
			datePicker.updateDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
		}
		
		long time = this.getIntent().getExtras().getLong(PARAM_TIME);
		if(time>0){
			TimePicker timePicker = (TimePicker) this.findViewById(R.id.ta_task_date_set_time);
			CheckBox enable = (CheckBox) this.findViewById(R.id.ta_task_date_set_time_enable);
			enable.setChecked(true);
			timePicker.setEnabled(true);
			
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeZone(TimeZone.getTimeZone("GMT+0"));
			calendar.setTimeInMillis(ComUtil.parseDateByUS(time).getTime());		
			timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
			timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
		}
	}

	@Override
	void refresh() {
		
	}
}
